package com.conv.shop.business.controller;

import com.conv.shop.business.domain.Order;
import com.conv.shop.business.req.TokenInfo;
import com.conv.shop.business.resp.OrderResp;
import com.conv.shop.business.resp.StoreGetOrderDetailResp;
import com.conv.shop.business.service.OrderService;
import com.conv.shop.business.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService service;

    /**
     * 订单生成接口
     * @param request
     * @return Order
     */
    @GetMapping("/make")
    public Order OrderMake(HttpServletRequest request){
        String token = request.getHeader("token");
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        Order order = service.OrderMaker(consumerId);
        if(order ==null){
            return null;
        }
        return order;
    }

    /**
     * 订单列表接口
     * @param tokenInfo
     * @return List<OrderResp>
     */
    @PostMapping("/list")
    public List<OrderResp> getOrderList(@RequestBody TokenInfo tokenInfo){
        String token = tokenInfo.getToken();
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        return service.getOrderList(consumerId);
    }

    /**
     * 查询商品id
     * @param orderNumber
     * @return int
     */
    @GetMapping("/productId")
    public int getProductId(String orderNumber){
        return service.getProductId(orderNumber);
    }

    /**
     * 商铺查看订单列表
     * @param request
     * @return List<StoreGetOrderDetailResp>
     */
    @GetMapping("/listOrderDetailStore")
    public ResponseEntity<List<StoreGetOrderDetailResp>> StoreOrderDetail(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null){
            return ResponseEntity.badRequest().body(null);
        }
        if(!JwtUtil.validate(token)){
            return ResponseEntity.badRequest().body(null);
        }
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        List<StoreGetOrderDetailResp> storeGetOrderDetailResps = service.getOrderDetailStore(consumerId);
        return ResponseEntity.ok().body(storeGetOrderDetailResps);
    }


}
